package gdconf

import (
	"gorm.io/gorm"
	"server_cluster/common/logger"
	"server_cluster/common/tools"
	"strconv"
	"strings"
)

// 常量配置表

type ConstConfig struct {
	ConstName  string `csv:"const_key" gorm:"primary_key;autoIncrement:false"` // 常量名
	ConstValue string `csv:"const_value,omitempty"`                            // 常量值
}

func (c *ConstConfig) TableName() string {
	return "config_const"
}

func (g *GameDataConfig) saveConstConfig() {
	saveTableToDb[ConstConfig](g.Db, readTable[ConstConfig](g.CsvPathPrefix+"constant.csv"))
}

func (g *GameDataConfig) loadConstConfig() {
	g.GameDataMaps.ConstConfigMap = make(map[string]*ConstConfig)
	for _, constConfig := range loadTableFromDb[ConstConfig](g.Db) {
		g.GameDataMaps.ConstConfigMap[constConfig.ConstName] = constConfig
	}
	logger.Info("ConstConfig Count: %v", len(g.GameDataMaps.ConstConfigMap))
}

func GetConstConfig(constName string) *ConstConfig {
	return CONF.GameDataMaps.ConstConfigMap[constName]
}

func GetConstConfigMap() map[string]*ConstConfig {
	return CONF.GameDataMaps.ConstConfigMap
}

func GetConstStr(name string) string {
	constConfig, exist := CONF.GameDataMaps.ConstConfigMap[name]
	if !exist {
		logger.Error("[ConfigNotFound] const not exist, name: %v", name)
		return ""
	}
	return constConfig.ConstValue
}

func GetConstInt(name string) int64 {
	constConfig, exist := CONF.GameDataMaps.ConstConfigMap[name]
	if !exist {
		logger.Error("[ConfigNotFound] const not exist, name: %v", name)
		return 0
	}
	constInt, err := strconv.ParseInt(constConfig.ConstValue, 10, 64)
	if err != nil {
		logger.Error("[ConfigNotFound] const format error, name: %v, error: %v", name, err)
		return 0
	}
	return constInt
}

func GetConstFloat(name string) float64 {
	constConfig, exist := CONF.GameDataMaps.ConstConfigMap[name]
	if !exist {
		logger.Error("[ConfigNotFound] const not exist, name: %v", name)
		return 0
	}
	constFloat, err := strconv.ParseFloat(constConfig.ConstValue, 64)
	if err != nil {
		logger.Error("[ConfigNotFound] const format error, name: %v, error: %v", name, err)
		return 0.0
	}
	return constFloat
}

func GetConstIntList(name string) []int32 {
	constConfig, exist := CONF.GameDataMaps.ConstConfigMap[name]
	if !exist {
		logger.Error("[ConfigNotFound] const not exist, name: %v", name)
		return nil
	}
	var strList = strings.Split(constConfig.ConstValue, "|")
	if len(strList) == 0 {
		return nil
	}
	intList := make([]int32, 0)
	for _, str := range strList {
		atoi, err := strconv.Atoi(str)
		if err != nil {
			logger.Error("[ConfigNotFound] const format error, name: %v, error: %v", name, err)
		}
		intList = append(intList, int32(atoi))
	}
	return intList
}
func GetConstTime(name string) int64 {
	constConfig, exist := CONF.GameDataMaps.ConstConfigMap[name]
	if !exist {
		logger.Error("[ConfigNotFound] const not exist, name: %v", name)
		return 0
	}
	return tools.Str2timestamp(constConfig.ConstValue)
}

func GetServerStartTime(conn *gorm.DB) int64 {
	var constConfig ConstConfig
	constConfig.ConstName = "SEVERSTARTDATE"
	result := conn.Model(constConfig).First(&constConfig)
	if result.Error != nil {
		return 0
	}
	return tools.Str2timestamp(constConfig.ConstValue)
}
